package ar.edu.uba.fi.jmit.monitorengine.backend.server;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.integrationcommons.EventPriority;
import ar.edu.uba.fi.jmit.monitorengine.MonitorEngineTestCase;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.StatisticPair;
import ar.edu.uba.fi.jmit.monitorengine.core.EventServiceManager;
import ar.edu.uba.fi.jmit.monitorengine.mock.EventServiceManagerSpy;

public class StatisticsServiceTestCase extends MonitorEngineTestCase {
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		EventServiceManagerSpy.useSpy();
		Calendar calendar = new GregorianCalendar();
		calendar.add(Calendar.SECOND, -10 * 13);
		for (int index = 1; index <= 13; index++) {
			Event event = new Event();
			event.setPriority(EventPriority.NORMAL);
			event.setSource("EventSource" + index);
			((EventServiceManagerSpy)EventServiceManager.getInstance()).setNextDate(calendar.getTime());
			EventServiceManager.getInstance().onRecivedEvent(event);
			calendar.add(Calendar.SECOND, 10);
		}		
	}
	
	@Override
	protected void tearDown() throws Exception {
		EventServiceManagerSpy.stopUsingSpy();
		EventServiceManager.getInstance().cleanEvents();
	}
	
	public void testGetAllReceiverStatistics() {
		StatisticsServiceImpl service = new StatisticsServiceImpl();
		List<StatisticPair> list = service.getAllReceiverStatistics();
		assertEquals(4 , list.size());
		//Total messages
		assertEquals(13, Integer.valueOf(list.get(0).getValue()).intValue());
		//Messages in last 10 minutes
		assertEquals(13, Integer.valueOf(list.get(1).getValue()).intValue());
		//Messages in last minute
		assertEquals(5, Integer.valueOf(list.get(2).getValue()).intValue());
		//Average messages per minute
		assertEquals(6, Integer.valueOf(list.get(3).getValue()).intValue());
	}

}
